<?php

import('@.Common.AdminAccount');
class AccountAction extends Action{
    protected function _initialize(){
        $this->assign('self', AdminAccount::get()->getInfo());
    }
    public function login(){
        if (AdminAccount::get()->getAdmin()){
        //已经登录过了
            $this->redirect('index/');
            return;
        }
        
        $AM= D('Account');
        $this->setJump();
        if (isset($_POST['submit'])){
            //TODO: login check            
            $u= AdminAccount::get()->login($_POST['username'], $_POST['password']);
            if ($u[0] <=0){
                if (Account::C('LOGIN_ERROR_USERNAME')==$u[0] ||Account::C('LOGIN_ERROR_PASSWORD')==$u[0]){
                    $this->error("用户名或密码错误");
                } else if (Account::C('LOGIN_ERROR_ANSWER')==$u[0]){
                    $this->error("安全提问错误");
                } else{
                    $this->error("请重新登录");
                }
            } else{
                if (! AdminAccount::get()->getAdmin()){
                    $this->error("没有管理员权限");
                } else{
                    $this->setJump();
                    $this->success("登录成功" );
                }
            }
        } else{
            $this->display();
        }
    }
    public function logout(){
        if (AdminAccount::get()->logout($_GET['ck'])){
            $this->setJump();
            $this->success("已安全注销");
        } else{
            $this->redirect('index/');
        }
    }
    protected function setJump(){
        $jumpUrl= $_GET['from'];
        if (! $jumpUrl){
            $jumpUrl= U('index/');
        }
        $this->assign("jumpUrl", $jumpUrl);
    }
}
?>